package com.example.armmvvm.ui.customdraw.view

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.MotionEvent.ACTION_MOVE
import com.example.arm.ext.dp
import timber.log.Timber

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.customdraw.view
 * @createTime 创建时间 ：2022/2/26
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */

private val RADIUS = 25.dp

private val PADDING = 25.dp

class CircleView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseCustomView(context, attrs, defStyleAttr) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val size = (RADIUS + PADDING) * 2
        // resolveSize 与 resolveSizeAndState 的区别::是否会保留前面两位的测量MODE,前者不会,后者会
        val width = resolveSize(size.toInt(), widthMeasureSpec)
        val height = resolveSize(size.toInt(), heightMeasureSpec)
        val width2 = resolveSizeAndState(size.toInt(), widthMeasureSpec, widthMeasureSpec)
        val height2 = resolveSizeAndState(size.toInt(), heightMeasureSpec, heightMeasureSpec)
        Timber.d("--${Integer.toHexString(width)}--${Integer.toHexString(height)}--${Integer.toHexString(width2)}--${Integer.toHexString(height2)}")
        setMeasuredDimension(width2, height2)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawCircle(width / 2f, height / 2f, RADIUS, paint)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == ACTION_MOVE) {
            return true
        }
        Timber.d("----action--------${Integer.toHexString(event.action)}")
        Timber.d("----actionMasked--${Integer.toHexString(event.actionMasked)}")
        return true
    }
}